package com.wangzaiplus.test.juc.thread.semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName SemaphoreDemo
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/29 11:12
 * @Version V1.0
 **/
public class SemaphoreDemo {
    static Semaphore semaphore=new Semaphore(2);
    public  static class T1 extends  Thread{
        public  T1(String name){
            super(name);
        }

        @Override
        public void run() {
            Thread thread = Thread.currentThread();
            try {
                semaphore.acquire();
                System.out.println(System.currentTimeMillis()+","+this.getName()+" 获取许可");
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                semaphore.release();
                System.out.println(System.currentTimeMillis()+","+this.getName()+" 释放许可");

            }



        }

        public static void main(String[] args){
            for (int i = 0; i < 10; i++) {
               new T1("t1-"+i).start();
            }
        }
    }
}
